﻿using Comm520;
using RFIDSystem.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RFIDSystem
{
    public partial class ProductMain : BaseForm
    {
        DLC520Socket dlcsocket;
        public ProductMain()
        {
            InitializeComponent();
            dlcsocket = this.GetSocket(SocketPort);
            dlcsocket.DataReceived += new DLC520Socket.MachineDataReceived(DataReceived);
        }

        private void ProductMain_Load(object sender, EventArgs e)
        {
            this.BindDropDownList();
            this.BindProductList();
        }

        private void BindProductList()
        {
            var products = from p in DAL.product_product
                           select new
                           {
                               p.product_id,
                               p.product_barcode,
                               p.product_catalog.product_catalog_name,
                               p.product_cost,
                               p.product_model,
                               p.product_name,
                               p.product_price,
                               p.product_rfid,
                               
                           };
            gridProductList.AutoGenerateColumns = false;
            gridProductList.DataSource = products.ToList();
        }

        private void BindDropDownList()
        {
            var productCatalog = DAL.product_catalog.ToList();
            this.cbProductCatalog.DisplayMember = "product_catalog_name";
            this.cbProductCatalog.ValueMember = "product_catalog_id";
            this.cbProductCatalog.DataSource = productCatalog;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
           
            product_product product = new product_product();
            product_catalog pcatalog = new product_catalog();

            pcatalog = DAL.product_catalog.Where(p => p.product_catalog_id == (int)cbProductCatalog.SelectedValue).FirstOrDefault();
            
            product.product_catalog = pcatalog;
            product.product_name = txtProductName.Text;
            product.product_model = txtProductModel.Text;
            product.product_barcode = txtProductBarcode.Text;
            product.product_rfid = txtProductRFID.Text;
            product.product_cost = decimal.Parse(txtProductCost.Text);
            product.product_price = decimal.Parse(txtProductPrice.Text);
            product.create_datetime = DateTime.Now;
            DAL.product_product.Add(product);
            DAL.SaveChanges();
            BindProductList();
        }

        bool DataReceived(string data)
        {
            try
            {

                //datetime=20160507163510,ip=192.168.1.10,result=15,event=1,cardx4=54585382,mdno=487,redio=0,photono=32,
                string cardid = "", ip = "", datetime = "", occur = "", result = "", before = "", after = "", mdno = "", cdno = "", dealtype = "", macno = "", serno = "", status = "";
                string[] items = data.Split(',');
                for (int index = 0; index < items.Length; index++)
                {
                    if (items[index].IndexOf("cardid", 0) >= 0 || items[index].IndexOf("cardx4", 0) >= 0)
                    {
                        cardid = items[index].Substring(7);
                    }
                    if (items[index].IndexOf("ip", 0) >= 0)
                    {
                        ip = items[index].Substring(3);
                    }
                    if (items[index].IndexOf("datetime", 0) >= 0)
                    {
                        datetime = items[index].Substring(9);
                    }
                    if (items[index].IndexOf("occur", 0) >= 0)
                    {
                        occur = items[index].Substring(6);
                    }
                    if (items[index].IndexOf("result", 0) >= 0)
                    {
                        result = items[index].Substring(7);
                    }
                    if (items[index].IndexOf("before", 0) >= 0)
                    {
                        before = items[index].Substring(7);
                    }
                    if (items[index].IndexOf("after", 0) >= 0)
                    {
                        after = items[index].Substring(6);
                    }
                    if (items[index].IndexOf("mdno", 0) >= 0)
                    {
                        mdno = items[index].Substring(5);
                    }
                    if (items[index].IndexOf("cdno", 0) >= 0)
                    {
                        cdno = items[index].Substring(5);
                    }
                    if (items[index].IndexOf("dealtype", 0) >= 0)
                    {
                        dealtype = items[index].Substring(9);
                    }
                    if (items[index].IndexOf("status", 0) >= 0)
                    {
                        status = items[index].Substring(7);
                    }
                    if (items[index].IndexOf("macno", 0) >= 0)
                    {
                        macno = items[index].Substring(6);
                    }
                    if (items[index].IndexOf("serno", 0) >= 0)
                    {
                        serno = items[index].Substring(6);
                    }

                }

                this.Invoke(new Action<TextBox, string>((t, s) => t.Text = s), new object[] { txtProductRFID, cardid });
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        private void gridProductList_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewRow row = gridProductList.CurrentRow;
            cbProductCatalog.SelectedIndex = cbProductCatalog.FindString(row.Cells["ProductCatalog"].Value.ToString());

            txtProductName.Text= row.Cells["ProductName"].Value.ToString();
            txtProductModel.Text = row.Cells["ProductModel"].Value.ToString();
            txtProductBarcode.Text = row.Cells["ProductBarcode"].Value.ToString();
            txtProductRFID.Text = row.Cells["ProductRFID"].Value.ToString();
            txtProductCost.Text = row.Cells["ProductCost"].Value.ToString();
            txtProductPrice.Text = row.Cells["ProductSalePrice"].Value.ToString();

        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            int currentId = (int)gridProductList.CurrentRow.Cells["ProductId"].Value;
            var product = DAL.product_product.Find(currentId);

            var pcatalog = DAL.product_catalog.Where(p => p.product_catalog_id == (int)cbProductCatalog.SelectedValue).FirstOrDefault();

            product.product_catalog = pcatalog;
            product.product_name = txtProductName.Text;
            product.product_model = txtProductModel.Text;
            product.product_barcode = txtProductBarcode.Text;
            product.product_rfid = txtProductRFID.Text;
            product.product_cost = decimal.Parse(txtProductCost.Text);
            product.product_price = decimal.Parse(txtProductPrice.Text);
            product.create_datetime = DateTime.Now;
           
            DAL.SaveChanges();
            BindProductList();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int currentId = (int)gridProductList.CurrentRow.Cells["ProductId"].Value;
            var product = DAL.product_product.Find(currentId);

            if (product.sale_order.Count > 0)
            {
                MessageBox.Show("该产品已经存在订单信息，不能删除");
                return;
            }

            if (product.stock_in.Count > 0)
            {
                MessageBox.Show("该产品已经存在入库单信息，不能删除");
                return;
            }

            if (product.stock_check.Count > 0)
            {
                MessageBox.Show("该产品已经存在盘点信息，不能删除");
                return;
            }

            DAL.product_product.Remove(product);
            DAL.SaveChanges();
            BindProductList();
        }
    }
}
